<template>
  <div class="back">
    <transition name="back">     
      <div class="back-top" @click="backTop" v-show="showBackTop">︿</div>
    </transition>
  </div>
</template>

<script>
export default {
  data () {
    return {
      showBackTop: false
    }
  },
  mounted () {
    window.addEventListener('scroll', this._handleScroll)
  },
  methods: {
    backTop () {
      var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      var timer = setInterval(function () {
        if (scrollTop <= 0) {
          scrollTop = 0
          clearInterval(timer)
        } else {
          scrollTop -= 70
        }
        document.documentElement.scrollTop = scrollTop
        document.body.scrollTop = scrollTop
        window.pageYOffset = scrollTop
      }, 10)
    },
    _handleScroll () {
      var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      if (scrollTop > 150) {
        this.showBackTop = true
      } else {
        this.showBackTop = false
      }
    }
  }
}
</script>

<style lang="scss">
  .back-top{
    position: fixed;
    bottom: 70px;
    right: 20px;
    z-index: 999999;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    text-align: center;
    line-height: 30px;
    background-color: rgb(0, 120, 222);
    opacity: 0.8;
    color: #fff;
    font-size: 20px;
    &.back-enter-active,
    &.back-leave-active {
      transition: all 0.6s linear
    }
    &.back-enter,
    &.back-leave-to {
      opacity: 0;
    }
    &.back-enter-to,
    &.back-leave {
      opacity: 0.8;
    }
  }
</style>
